package com.example.fmgidsprototype.data;

import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.provider.BaseColumns;

import com.example.fmgidsprototype.data.VenueLocationTable.VenueLocationColumns;
import com.example.fmgidsprototype.model.VenueLocation;

public class VenueLocationDao implements Dao<VenueLocation>{
	
	private SQLiteDatabase db;
	private SQLiteStatement insertStatement;
	private static final String INSERT = "insert into " + VenueLocationTable.TABLE_NAME +
			" ( " + VenueLocationColumns.VENUE + ", " + VenueLocationColumns.LONGITUDE +
			", " + VenueLocationColumns.LATITUDE + ", " + VenueLocationColumns.DISTANCE_FROM_ZERO + 
			" ) values (?,  ?, ?, ?)";
	
	public VenueLocationDao(SQLiteDatabase db){
		this.db = db;
		this.insertStatement = db.compileStatement(INSERT);
	}

	@Override
	public long save(VenueLocation venueLocation) {
		insertStatement.clearBindings();
		insertStatement.bindLong(1, venueLocation.getVenueId());
		insertStatement.bindDouble(2, venueLocation.getLongitude());
		insertStatement.bindDouble(3, venueLocation.getLatitude());
		if(venueLocation.getDistanceFromZero() != -1)
			insertStatement.bindDouble(4, venueLocation.getDistanceFromZero());
		try{
			return insertStatement.executeInsert();
		}
		catch(Exception e){
			return -1;
		}
	}

	@Override
	public long update(VenueLocation venueLocation) {
		ContentValues values = new ContentValues();
		values.put(VenueLocationColumns.VENUE, venueLocation.getVenueId());
		values.put(VenueLocationColumns.LONGITUDE, venueLocation.getLongitude());
		values.put(VenueLocationColumns.LATITUDE, venueLocation.getLatitude());
		values.put(VenueLocationColumns.DISTANCE_FROM_ZERO, venueLocation.getDistanceFromZero());
		try{
			return db.update(VenueLocationTable.TABLE_NAME, values, VenueLocationColumns.VENUE + " = ?",
					new String[]{String.valueOf(venueLocation.getVenueId())});
		}
		catch(Exception e){
			return -1;
		}
	}

	@Override
	public void delete(VenueLocation venueLocation) {
		if(venueLocation.getId() > 0){
			db.delete(VenueLocationTable.TABLE_NAME, BaseColumns._ID + " = ?", 
					new String[]{String.valueOf(venueLocation.getId())});
		}
	}

	@Override
	public VenueLocation get(long id) {
		VenueLocation venueLocation = null;
		Cursor c = db.query(VenueLocationTable.TABLE_NAME, new String[]{
				BaseColumns._ID, VenueLocationColumns.VENUE, VenueLocationColumns.LONGITUDE,
				VenueLocationColumns.LATITUDE, VenueLocationColumns.DISTANCE_FROM_ZERO
		}, BaseColumns._ID + " = ?", new String[]{String.valueOf(id)}, null, null, null);
		if(c.moveToFirst()){
			venueLocation = this.createFromCursor(c);
		}
		if(!c.isClosed())
			c.close();
		return venueLocation;
	}
	
	public VenueLocation getLocationFromVenue(long venueId){
		VenueLocation venueLocation = null;
		Cursor c = db.query(VenueLocationTable.TABLE_NAME, new String[]{
				BaseColumns._ID, VenueLocationColumns.VENUE, VenueLocationColumns.LONGITUDE,
				VenueLocationColumns.LATITUDE, VenueLocationColumns.DISTANCE_FROM_ZERO	
		}, VenueLocationColumns.VENUE + " = ?", new String[]{String.valueOf(venueId)}, null, null, null);
		if(c.moveToFirst())
			venueLocation = this.createFromCursor(c);
		if(!c.isClosed())
			c.close();
		return venueLocation;
	}

	@Override
	public List<VenueLocation> getAll() {
		// TODO Auto-generated method stub
		return null;
	}
	
	private VenueLocation createFromCursor(Cursor c){
		VenueLocation venueLocation = null;
		if(c != null){
			venueLocation = new VenueLocation();
				venueLocation.setId(c.getLong(0));
				venueLocation.setVenueId(c.getLong(1));
				venueLocation.setLongitude(c.getDouble(2));
				venueLocation.setLatitude(c.getDouble(3));
				venueLocation.setDistanceFromZero(c.getDouble(4));
		}
		return venueLocation;
	}
	
	public void deleteAll(){
		String deleteSql = "delete from " + VenueLocationTable.TABLE_NAME ;
		db.execSQL(deleteSql);
	}

}
